import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * service HR组织架构维护
 * @date: 2018-6-19

s
 */
import request from 'utils/request';
import { getCurrentOrganizationId, parseParameters } from 'utils/utils';
import { CPAAS_PLATFORM, CPAAS_IAM } from 'utils/config';
/**
 * 请求API前缀
 * @type {string}
 */

var prefix = "".concat(CPAAS_PLATFORM, "/v1");
/**
 * 获取租户信息
 * @async
 * @function fetchOrgInfo
 * @returns {object} fetch Promise
 */

export function fetchOrgInfo() {
  return _fetchOrgInfo.apply(this, arguments);
}
/**
 * 更新组织信息
 * @async
 * @function saveEdit
 * @param {Object} params - 请求参数
 * @param {!string} params.tenantId - 租户Id
 * @param {!object} params.values - 待保存的组织信息对象
 * @param {!string} params.values.unitId - 组织Id
 * @param {!string} params.values.unitCode - 组织编码
 * @param {!string} params.values.unitName - 组织名称
 * @param {!string} params.values.unitTypeCode - 组织类型编码
 * @param {?string} params.values.supervisorFlag - 主管组织标记
 * @param {?string} params.values.enabledFlag - 启用标记
 * @param {?string} params.values.parentUnitId - 上级组织Id
 * @returns {object} fetch Promise
 */

function _fetchOrgInfo() {
  _fetchOrgInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            return _context.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(getCurrentOrganizationId(), "/tenants"), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _fetchOrgInfo.apply(this, arguments);
}

export function saveEdit(_x) {
  return _saveEdit.apply(this, arguments);
}
/**
 * 添加组织信息
 * @async
 * @function saveAdd
 * @param {Object} params - 请求参数
 * @param {!string} params.tenantId - 租户Id
 * @param {!object[]} params.data - 新增组织对象列表
 * @param {!string} params.data[].unitId - 组织Id
 * @param {!string} params.data[].unitCode - 组织编码
 * @param {!string} params.data[].unitName - 组织名称
 * @param {!string} params.data[].unitTypeCode - 组织类型编码
 * @param {?number} [params.data[].supervisorFlag = 0 ] - 主管组织标记
 * @param {?number} [params.data[].enabledFlag = 1 ] - 启用标记
 * @param {?string} params.data[].parentUnitId - 上级组织Id
 * @returns {object} fetch Promise
 */

function _saveEdit() {
  _saveEdit = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units"), {
              method: 'PUT',
              body: _objectSpread({}, params.values)
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _saveEdit.apply(this, arguments);
}

export function saveAdd(_x2) {
  return _saveAdd.apply(this, arguments);
}
/**
 * 禁用组织信息
 * @async
 * @function forbindLine
 * @param {Object} params - 请求参数
 * @param {!string} params.tenantId - 租户Id
 * @param {!string} params.unitId - 组织Id
 * @param {!string} params._token - token
 * @param {!number} params.objectVersionNumber - 版本号
 * @returns {object} fetch Promise
 */

function _saveAdd() {
  _saveAdd = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units"), {
              method: 'POST',
              body: _toConsumableArray(params.data)
            }));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _saveAdd.apply(this, arguments);
}

export function forbindLine(_x3) {
  return _forbindLine.apply(this, arguments);
}
/**
 * 启用组织信息
 * @async
 * @function enabledLine
 * @param {!string} params.tenantId - 租户Id
 * @param {!string} params.unitId - 组织Id
 * @param {!string} params._token - token
 * @param {!number} params.objectVersionNumber - 版本号
 * @returns {object} fetch Promise
 */

function _forbindLine() {
  _forbindLine = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units/disable"), {
              method: 'POST',
              body: _objectSpread({}, params)
            }));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _forbindLine.apply(this, arguments);
}

export function enabledLine(_x4) {
  return _enabledLine.apply(this, arguments);
}
/**
 * 无分页懒加载查询组织信息
 * 第一次 查询所有组织
 * 第二次 依据组织id 查询该组织下的直接下级组织
 * @param {{uniId?: number}} params - 查询参数
 * @return {Promise<object>}
 */

function _enabledLine() {
  _enabledLine = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units/enable"), {
              method: 'POST',
              body: _objectSpread({}, params)
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _enabledLine.apply(this, arguments);
}

export function organizationQueryLazyTree() {
  return _organizationQueryLazyTree.apply(this, arguments);
}
/**
 * 分页查询所有组织信息
 * @param {{uniId?: number}} params - 查询参数
 * @return {Promise<object>}
 */

function _organizationQueryLazyTree() {
  _organizationQueryLazyTree = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
    var params,
        _args6 = arguments;
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            params = _args6.length > 0 && _args6[0] !== undefined ? _args6[0] : {};
            return _context6.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(getCurrentOrganizationId(), "/units/company/lazy-tree"), {
              method: 'GET',
              query: params
            }));

          case 2:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _organizationQueryLazyTree.apply(this, arguments);
}

export function organizationQueryLine() {
  return _organizationQueryLine.apply(this, arguments);
}

function _organizationQueryLine() {
  _organizationQueryLine = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
    var params,
        parsedParams,
        _args7 = arguments;
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            params = _args7.length > 0 && _args7[0] !== undefined ? _args7[0] : {};
            parsedParams = parseParameters(params);
            return _context7.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(getCurrentOrganizationId(), "/units/list"), {
              method: 'GET',
              query: parsedParams
            }));

          case 3:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _organizationQueryLine.apply(this, arguments);
}